package com.tjp.eight.ui.fragment.zuji;

import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.ToatUtils;
import com.tjp.eight.views.rcview.EndlessRecyclerOnScrollListener;
import com.tjp.eight.views.rcview.HeaderViewRecyclerAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/27.
 */
public class FragmentZuJi extends BaseFragment<ZuJiPresenter> implements ZuJiView {
    @Bind(R.id.rv_fragment_history)
    RecyclerView rv_fragment_history;

    private int screenHeight = 0;
    private int screenWidth = 0;
    private String cid;
    private int page = 1;

    HeaderViewRecyclerAdapter headerViewRecyclerAdapter;
    View footView;
    HistoryAdapter adapter;

    @Override
    protected ZuJiPresenter createPresenter() {
        return new ZuJiPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_history, container, false);
        }
        ButterKnife.bind(this, contentView);
        initViews();
        cid = getActivity().getIntent().getStringExtra("cid");
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loadData(getPJson());
    }

    private String getPJson() {
        HistoryPModel model = new HistoryPModel();
        model.cid = cid;
        model.page = page;

        return new Gson().toJson(model);
    }

    private void initViews() {
        rv_fragment_history.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        rv_fragment_history.addOnScrollListener(new EndlessRecyclerOnScrollListener(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false)) {
            @Override
            public void onLoadMore(int currentPage) {
                page = currentPage;
                presenter.loadMData(getPJson());
            }
        });
        footView = LayoutInflater.from(getContext()).inflate(R.layout.swipe_refresh_footer, rv_fragment_history, false);
        adapter = new HistoryAdapter(getActivity());
        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(adapter);
        rv_fragment_history.setAdapter(headerViewRecyclerAdapter);
        initHeight();
    }

    private void initHeight() {
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        screenHeight = point.y;
        screenWidth = point.x;
    }

    void hideFooter(int total) {
        int height = rv_fragment_history.getMeasuredHeight();
        if (height <= screenHeight) {
            headerViewRecyclerAdapter.removeFootByIndex(0);
        } else {
            if (adapter.getItemCount() >= total) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            } else if (headerViewRecyclerAdapter.getFooterCount() == 0) {
                headerViewRecyclerAdapter.addFooterView(footView);
            }
        }
    }

    @Override
    public void loadSuccess(HistoryRModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result)) {
                if (model.data != null)
                    adapter.addData(model.data.list);
            } else {
                ToatUtils.toastCommentFail(getActivity(), model.msg);
            }
            if (model.data != null)
                hideFooter(model.data.total_items);
        }

    }

    @Override
    public void fail(String msg) {

    }

    @Override
    public void loadMSuccess(HistoryRModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result)) {
                if (model.data != null)
                    adapter.addData(model.data.list);
            } else {
                ToatUtils.toastCommentFail(getActivity(), model.msg);
            }
            if (model.data != null)
                hideFooter(model.data.total_items);
        }
    }
}
